/**
 * 
 */
package server.conf;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Properties;


/**
 * Module that manages loaded from a file and default preferences.
 * Configuration file is laid in conf/preferences.properties. 
 * 
 * Preferences module is implemented as a standalone Preferences object.  
 * 
 * @author Kamil Nowak
 *
 */
public class Preferences {

	//private final static Logger logger = Logging.instance().getLogger("preferences");
	
	private static final String PREFERENCE_FILE = "conf/preferences.properties"; 
	
	private Properties properties = new Properties();
	
	private static final Preferences instance = new Preferences();
	
	private final Hashtable<String,Prop> DEFAULT_PREFERENES = new Hashtable<String,Prop>();
	
	{
		DEFAULT_PREFERENES.put("default_threads_number",new Prop("30",Integer.class));
		DEFAULT_PREFERENES.put("max_threads_number",new Prop("300",Integer.class));
		DEFAULT_PREFERENES.put("keep_alive_time",new Prop("1000",Integer.class));
		DEFAULT_PREFERENES.put("connections_per_thread",new Prop("30",Integer.class));
		DEFAULT_PREFERENES.put("host",new Prop("localhost",String.class));
		DEFAULT_PREFERENES.put("port",new Prop("18018",Integer.class));
		DEFAULT_PREFERENES.put("buffer_size",new Prop("1024",Integer.class));
		DEFAULT_PREFERENES.put("encoding",new Prop("ISO-8859-2",String.class));
		DEFAULT_PREFERENES.put("log_level",new Prop("all",String.class));
		DEFAULT_PREFERENES.put("log_pattern",new Prop("%d [%t] %-5p %c - %m%n",String.class));
		DEFAULT_PREFERENES.put("log_dir",new Prop("log",String.class));
		
	}
	
	private static class Prop {
		String value;
		Class type;
		Prop(String value, Class type) {
			this.value = value;
			this.type = type;
		}
	}
	
	private Preferences() {
		init(); 
	}

	private void init() {
		try {
			FileInputStream in = new FileInputStream(PREFERENCE_FILE);
			properties.load( in );
			System.out.println("Preferences file loaded " + PREFERENCE_FILE);
			in.close();
		} catch (FileNotFoundException e) {
			System.err.println("Preferences file cannot be loaded " + PREFERENCE_FILE + " " + e.getMessage());
		} catch (IOException e) {
			System.err.println("Preferences file cannot be loaded " + PREFERENCE_FILE + " " + e.getMessage());
		}
	}
	
	/**
	 * Gets standalone instance of Preferences class
	 * @return standalone object
	 */
	public static Preferences instance() {
		return instance;
	}
	
	/**
	 * Returns parameter value. 
	 * If parameter is defined in a configuration file it's value is returned, otherwise method returns default value.
	 * For number parameters validation is performed. If it fails default value is returned.
	 * 
	 * @param key name of the preference
	 * @return value of the preference
	 */
	public String getPreference(String key) {
		Prop retVal = DEFAULT_PREFERENES.get(key);
		String prop = properties.getProperty(key);
		if (prop == null) return retVal.value;
		
		try {
			if (retVal.type == Integer.class)
				Integer.valueOf(prop);

		} catch (NumberFormatException e) {
			return retVal.value;
		}
		
		return prop;
	}
	
}
